
package com.bear.cmcc.NetTestHTTP.util;

import android.content.Context;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class TimeUtil {
    private static SimpleDateFormat month_format = new SimpleDateFormat("MM月dd日 HH:mm");

    /**
     * 日期字符串转换 pattern可以从R.string中获取，datetime_pattern_yyyymmddhhmmss,
     * datetime_pattern_yyyy_mm_dd_hhmmss，
     * date_pattern_chinese,datetime_pattern_chinese等
     * 
     * @param originalPattern 初始日期格式
     * @param targetPattern 目标日期格式
     * @param datetime 日期字符串
     * @return
     */
    public static String formatDateTime(Context context, int originalPatternStrId,
            int targetPatternStrId, String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat(context.getString(originalPatternStrId),
                Locale.getDefault());
        Date date = null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            sdf = new SimpleDateFormat(context.getString(targetPatternStrId), Locale.getDefault());
            String target = sdf.format(date);
            return target;
        }
        return null;

    }

    /**
     * @param time e.g. "2012-12-12 12:12:12"
     * @return Wed Dec 12 12:12:12 CST 2012
     */
    public static Date string2Date(String time) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            try {
                // 第二种格式方案
                date = dateFormat2.parse(time);
            } catch (ParseException e1) {
                e1.printStackTrace();
                return null;
            }
        }
        return date;
    }

    public static String long2String(long time) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date date = new Date(time);
        String str = dateFormat.format(date);
        return str;
    }

    /**
     * @param time
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String long2String2(long time) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(time);
        String str = dateFormat.format(date);
        return str;
    }

    /**
     * @param time yyyy-MM-dd
     * @return MM-dd
     */
    public static String string2String(String time) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd");
        Date date = string2Date(time);
        String str = dateFormat.format(date);
        return str;
    }

    /**
     * @param time yyyy-MM-dd
     * @return MM-dd
     */
    public static String secondBetweenTime(String startTime, String endTime) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long between = 0;
        try {
            java.util.Date begin = dfs.parse(startTime);
            java.util.Date end = dfs.parse(endTime);
            between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
        } catch (Exception e) {
            // TODO: handle exception
        }

        // long day1=between/(24*3600);
        // long hour1=between%(24*3600)/3600;
        // long minute1=between%3600/60;
        // long second1=between%60/60;

        return String.valueOf(between);
    }

    /**
     * @param time yyyy-MM-dd
     * @return MM-dd HH:mm:ss
     */
    public static String formatTime(String startTime) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = dfs.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return month_format.format(date);
    }

    public static int getCurrentHourOfDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        return calendar.get(Calendar.HOUR_OF_DAY);

    }

    /**
     * 获取随机时间[20:00--06:00]
     * @return
     */
    public static int getRandomServiceRunTime() {
        Random random = new Random();
        int rand = random.nextInt(10);

        if (rand == 9) {
            rand = 21;
        } else if (rand == 8) {
            rand = 22;
        } else if (rand == 7) {
            rand = 23;
        }
        return rand;
    }
}
